package cn.wolfcode.web.controller;


import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.service.IQuestionItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/questionItem")
public class QuestionItemController {

    @Autowired
    private IQuestionItemService questionItemService;


    @RequiredPermission(name="试卷答案表页面",expression="questionItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<QuestionItem> pageInfo = questionItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "questionItem/list";
    }


    @RequiredPermission(name="试卷答案表删除",expression="questionItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           questionItemService.delete(id);
        }
        return "redirect:/questionItem/list";
    }


    @RequiredPermission(name="试卷答案表新增/编辑",expression="questionItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(QuestionItem questionItem) {
        if (questionItem.getId() == null) {
            questionItemService.save(questionItem);
        } else {
            questionItemService.update(questionItem);
        }
        return "redirect:/questionItem/list";
    }


    @RequiredPermission(name="试卷答案表新增/编辑",expression="questionItem:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("questionItem",questionItemService.get(id));
        }
        return "questionItem/input";
    }
}
